package com.ash.bean.anno.service;

import com.ash.bean.anno.User;
import com.ash.bean.anno.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class UserService {

    private UserRepository userRepository;

    public UserService() {
        System.out.println("UserService init....");
        this.userRepository = userRepository;
    }

    //Autowired 会根据类型进行注入，如果有多个相同的类型 ，则会根据属性名或者set方法名来查找对应的bean，如果还不行，这时有两种选择方案
    //1.通过@Qualifier("userRepositoryImpl") 找到相应的bean
    //2.通过@Component("userRepository");
    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void add(User user) {
        System.out.println("userservice add");
        userRepository.add(user);
    }

}
